package com.common.example.config.service.impl;

import com.common.example.config.base.BaseDaoMapper;

import com.common.example.config.controller.vo.PageVo;
import com.common.example.config.entity.BaseCommonStringEntity;
import com.common.example.config.entity.IBaseCommonEntity;
import com.common.example.config.entity.ICommonEntity;
import com.common.example.config.service.IBaseCommonService;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * baseService
 * 
 * @param <T>
 *            需要操作的实体类
 */
public class BaseCommonServiceImpl<T> implements IBaseCommonService<T> {


	@Autowired
	protected BaseDaoMapper<T> baseDaoMapper;


	@SuppressWarnings("unchecked")
	public T initSaveEntity(T entity) {

		return entity;
	}

	@Override
	public T save(T entity) {
		entity = initSaveEntity(entity);
		baseDaoMapper.insertSelective(entity);
		return entity;
	}

	@Override
	public void save(Collection<T> entityList) {
		Assert.notNull(entityList);
		// 批量保存时,保证创建时间一致
		//Date createTime = DateUtil.getNowDateTime();
		List<T> entitys = new ArrayList<>();
		int count = 0;
		for (T entity : entityList) {
			count++;
			entity = initSaveEntity(entity);
			entitys.add(entity);
			if (count == 1000) {
				baseDaoMapper.insertList(entitys);
				entitys.clear();
				count = 0;
			}
		}
		if (!entitys.isEmpty()) {
			baseDaoMapper.insertList(entitys);
		}
	}

	@Override
	public int update(T entity) {

		return baseDaoMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public int updateIncludingNull(T entity) {

		return baseDaoMapper.updateByPrimaryKey(entity);
	}

	@Override
	public int update(Collection<T> entityList) {
		Assert.notNull(entityList);
		// 批量修改时,保证修改时间一致
		//Date updateTime = DateUtil.getNowDateTime();
		int result = 0;
		for (T entity : entityList) {
			update(entity);
		}
		return result;
	}

	public int update(T entity, String updateTime, String updatePerson) {
		return baseDaoMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public int updateByExample(T record, Example example) {
		return baseDaoMapper.updateByExample(record, example);
	}

	@Override
	public int delete(Object id) {
		Assert.notNull(id);
		return baseDaoMapper.deleteByPrimaryKey(id);
	}


	@Override
	public T findById(Serializable id) {
		return (T) baseDaoMapper.selectByPrimaryKey(id);
	}

	@Override
	public T findOne(T record) {
		return baseDaoMapper.selectOne(record);
	}

	@Override
	public List<T> findList(T record, PageVo pageVo) {
		if (pageVo != null) {
			PageHelper.startPage(pageVo.getPageIndex(), pageVo.getPageSize());
		}
		return baseDaoMapper.select(record);
	}

	@Override
	public List<T> findList(Example example, PageVo pageVo) {
		if (pageVo != null) {
			PageHelper.startPage(pageVo.getPageIndex(), pageVo.getPageSize());
		}
		return baseDaoMapper.selectByExample(example);
	}

	@Override
	public List<Map<?, ?>> findListForMap(Example example, PageVo pageVo) {
		return null;
	}


	@SuppressWarnings("unchecked")
	@Override
	public boolean isUnique(T record, String id) {
		boolean result = true;
		T entity = baseDaoMapper.selectOne(record);
		if (entity != null && entity instanceof IBaseCommonEntity) {
			IBaseCommonEntity<Serializable> dataEntity = (IBaseCommonEntity<Serializable>) entity;
			if (!dataEntity.getId().equals(id)) {
				result = false;
			}
		}
		return result;
	}

	@Override
	public int selectCount(T t) {
		return baseDaoMapper.selectCount(t);
	}



}
